<!DOCTYPE html>
<html lang="en">
	<head>
    	<meta name="layout" content="main_pc">
        <title>火堆工作台</title>       
        <link rel="stylesheet" href="${resource(dir: 'css', file: 'user.center.css')}" type="text/css">
        <link rel="stylesheet" href="${resource(dir: 'css_pc', file: 'project.css')}" type="text/css">
		<script type="text/javascript" src="${resource(dir: 'js', file: 'jquery-1.10.2.js')}"></script>
	    <script type="text/javascript" src="${resource(dir: 'js', file: 'ajaxfileupload.js')}"></script>
	    <style>
	    div .img{
	    	float: left;
	    	width: 220px; 
	    	min-height: 160px;
	    	background-size: cover;
	    	background-repeat: no-repeat;
	    }
	    div .del-btn{
	        float: left;
	        margin-top: 75px;
	    }
	    div .cover{
	    	display:block;
	    	float: left;
	    	margin-left:20px;
	    	margin-bottom:15px;
	    }
	    .form-ctrl .img-box-single input[type=file] {
		    position: absolute;
		    width: 100%;
		    height: 100%;
		    left: 0;
		    top: 0;
		    z-index: 100;
		    opacity: 0;
		}
		
	    .form-ctrl .img-box-m input[type=file] {
		    position: absolute;
		    width: 100%;
		    height: 100%;
		    left: 0;
		    top: 0;
		    z-index: 100;
		    opacity: 0;
		}
		.form-style3 .tips {
		    color: #FB6E50;
		}
	    </style>
    </head>
<body>

	<div class="container-fluid">

		<section class="quick-navi mb20">
			<div class="container">
				<a href="#" class="quick-link"><span>火堆工作台</span><span
					class="caret"></span></a>
				<!--a href="#" class="quick-link">联系我们</a-->
			</div>
		</section>

		<section>
			<div class="container mb50">
				
				<g:render template="navigate"   model="[status: status]"/>
				
				<div class="right-content" id="rightconsole">

					<div class="launch-container">
               
                <div class="content">
                	<h3>项目修改绑定爱心池</h3>
                    <form id = "all-form" action="/scfire/manage/saveBandFund" method="post" class="form-style3">
                    	<input type="hidden" name="projectid" value="${project.id }" id="projectid"/>
                    
                    
                        <div class="form-item fix-width">
                            <label for="">项目名称:<a href="/scfire/project/projectDetail?projectid=${project.id}" target="_blank">${project.name }</a></label>
                        </div>
                        <div class="form-item fix-width">
                            <label for="">项目筹款情况:
                            <g:formatNumber number="${project.amount - project.rest }" type="number" maxFractionDigits="2" />元(已筹) / 
                            <g:formatNumber number="${project.rest }" type="number" maxFractionDigits="2" />元(还需)</label>
                        </div>
                        
                        <div class="form-item fix-width">
                            <label for="">项目配捐情况:
	                            <g:if test="${fruler != null}">
	                            	<!-- 未结束 -->
	                            	<g:if test='${fruler?.status == "true"}'>
		                            	<span id="use"><g:formatNumber number="${fruler.bandfund/100 }" type="number" maxFractionDigits="2" /></span>元(待配捐)/
	                            	</g:if>
	                            	<!-- 已结束 -->
	                            	<g:if test='${fruler?.status == "false"}'>
	                            		<span id="use">0</span>元(待配捐)/
	                            	</g:if>
	                            	<span><g:formatNumber number="${fruler.donatefund/100}" type="number" maxFractionDigits="2" /></span>元(已配捐)
	                            </g:if>
	                            <g:else>
	                            	<span id="use">0.00</span>元(待配捐)
	                            	<span>0.00</span>元(已配捐)
	                            </g:else>
	                            
	                            
	                            
                            </label>
                        </div>
                        
                        <div class="form-item fix-width">
                            <label for="">选择爱心池：</label>
                            <div class="form-ctrl">
                                <div class="select-ctrl">
                                
                                	<!-- 如果存在配捐规则 ， 不能修改 -->
                                	<g:if test="${fruler != null }">
                                		<select id="types" name="poolid" onchange="onselectChange();" disabled="disabled">
                                    		<option value="${fruler.fundPool.id }" canUse="${(fruler.fundPool.totalCount - fruler.fundPool.donateAssigned)/100}" waitUse = "${(fruler.fundPool.totalCount - fruler.fundPool.donateActual)/100 }" >${fruler.fundPool.name }</option>
                                    	</select>
                                	</g:if>
                                	<g:else>
                                		<!-- 如果不存在配捐规则，新增配捐规则 -->
	                                	<select id="types" name="poolid" onchange="onselectChange();">
	                                    	<g:each in="${fundPools }" var="fundPool">
	                                    		<option value="${fundPool.id }" canUse="${(fundPool.totalCount - fundPool.donateAssigned)/100}" waitUse = "${(fundPool.totalCount - fundPool.donateActual)/100 }" >${fundPool.name }</option>
	                                    	</g:each>
	                                    </select>
                                	</g:else>
                                </div>
                            </div>
                        </div>
                        <div class="form-item fix-width">
                            <label for="">爱心池配捐余额:<span id="canUseDiv"></span>元(可用)
                        </div>
                        
                        <div class="form-item fix-width">
                            <label for="">操作类型：
                            	<input type="radio" name="operate" checked="checked" value="add" id="add"/>增加项目配捐
                            	<g:if test="${fruler != null }">
	                            	<input type="radio" name="operate" value="minus" id="minus"/>减少项目配捐
	                            	<input type="radio" name="operate" value="over" id="over"/>结束项目配捐
                            	</g:if>
                            </label>
                        </div>
                        
                        <div class="form-item fix-width">
                            <label for="">配捐金额：
                            	<input placeholder="请输入正数" type="text" value="" name="money" id="money"/>元
                            </label>
                        </div>
                        
                        
                        
                        
                        <div class="form-item fix-width">
                            <label for="">配捐比例：
                            	<input placeholder="用户金额（正整数）" type="text" value="" name="divisor" id="divisor"/> ： 
                            	<input placeholder="配捐金额（正整数）" type="text" value="" name="dividend" id="dividend"/>
                            </label>
                        </div>
                        
                        
                        
                        
                        
                        <div class="form-item fix-width">
                            <label for="">备注说明：
                            	<textarea placeholder="请详细说明这笔金额的来源、变动理由等关键信息" rows="5" cols="70" id="mark" name="mark" id="mark"></textarea>
                            </label>
                        </div>
                        
                        <div class="form-item">
				            <label for="#">短信验证码: <span class="tips">*</span>
				            	<input type="hidden" id="mobile" name="mobile" value="15152675666">
				                <input type="text" placeholder="验证码" id="authCode" style="width: 45%">
				                <button id="sendMsg" type="button" style="width: 15%">发送验证码</button>
				            </label>
				        </div>
                        
                        
                        <div class="form-item">
                            <div class="btn-container">
                                <Button type="button" class="btn btn-save-all" onclick="check();">保存</Button>
                            </div>
                        </div>
                    </form>
                    
                </div>
            </div>                           
            	</div>
                	
                	
			</div>
			<div class="clearboth"></div>
		</section>


	</div>
	<g:if test="${fruler }">
		<script type="text/javascript">
		//初始化fruler  regular
			var regular = '${fruler.regular}';
			if(regular!=''){
				var divisor = regular.split(":")[0];
				var dividend = regular.split(":")[1];
				$("#divisor").val(divisor);
				$("#dividend").val(dividend);
				$("#divisor").attr("disabled", true);
				$("#dividend").attr("disabled", true);
			}
		</script>
	</g:if>

	<script type="text/javascript">
		$('#sendMsg').click(function(){
	        //发送短信
	        var mobile = $('#mobile').val();
	        if(!sendSMS(mobile)){//号码格式验证成功即返回true
	            return;
	        };
	
	        var obj = $(this).attr('disabled', true);
	        var count = null, i = 60;
	        obj.text(i + '秒后可重发');
	        count = setInterval(function(){
	            i--;
	            if (i <= 0) {
	                obj.removeAttr('disabled').text('重发验证码');
	                clearInterval(count)
	            } else {
	                obj.text(i + '秒后可重发');
	            }
	        }, 1000)
	    });
		function onselectChange(){
			var canUse = jQuery("#types  option:selected").attr("canUse");
			var waitUse = jQuery("#types  option:selected").attr("waitUse");
			$("#canUseDiv").html(canUse);
			$("#waitUseDiv").html(waitUse);
		}
	    function sendSMS(mobile){

	        if(!checkPhoneNumber(mobile)){
	        	alert("号码格式不正确，短信发送失败.");
	        	return false;
	        }
	    	$.ajax({
				url:"/scfire/SmsVerification/sendSmsVerification",
				type:'POST',
				data:{
					  "mobile":mobile
					  },
				dataType:'text',
				async: false,
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				success:function(data){
					var result = JSON.parse(data).info.text;
					if(data.code=="200"){
						alert('短信发送成功.');
					}else{
						alert(result);
					}				
				},
				error: function (data){
					alert("短信发送失败.请稍后再试");
				}
			});
	    	return true;
	    }
	    function checkPhoneNumber(value){
	    	var mobile = value
	    	console.log(mobile)
	        if(!(/^1[3|5|8][0-9]\d{8}$/.test(mobile))){ 
	            //alert("手机号格式错误");
	            //isPhoneNumVailded = false;
	            return false; 
	        }else{
	        	//isPhoneNumVailded = true;
	            return true;
	        } 
	    }

	    function check(){
			var money = $("#money").val();
			var canUse = $("#canUseDiv").html().replace(",","");
			var waitUse = $("#waitUseDiv").html();
			
			var use = $("#use").html();
			if(use!="" && use!=null && use.indexOf(",")!=-1){
				use = $("#use").html().replace(/,/g,"");
				
			}
			
			var mark = $("#mark").val();

			//规则
			var divisor = $("#divisor").val();
			var dividend = $("#dividend").val();
			
			if($('#add').is(':checked') || $('#minus').is(':checked')){
				if(money == ""){
					alert("请输入配捐金额");
					return;
				}
				if(isNaN(money)){
					alert("配捐金额请输入数字");
					return;
				}
				if(!isNaN(money)){
			        var dot = money.indexOf(".");
			        if(dot != -1){
			            alert("配捐金额精确到元！");
			            return;
			        }
			    }
			    if(money <= 0){
					alert("配捐金额不能小于1元！");
					return;
				}
			}
			
			//如果add被选中 , 配捐金额不能大于配捐池余额
			if($('#add').is(':checked') && (parseInt(money) > parseInt(canUse))){
				alert("配捐金额不能大于配捐池余额！");
				return;
			}
			//如果minus被选中 , 减少金额不能大于已配捐金额
			if($('#minus').is(':checked') && (parseInt(money) > parseInt(use))){
				alert("配捐金额不能大于已配捐余额！");
				return;
			}

			

			if(divisor =="" || divisor==null || dividend =="" || dividend==null){
				alert("请完整输入配捐比例！");
				return;
			}

			if(isNaN(divisor) || isNaN(dividend)){
				alert("配捐比例请输入数字！");
				return;
			}
			if(!isNaN(divisor) && !isNaN(dividend)){
		        var dot = divisor.indexOf(".");
		        if(dot != -1){
		            alert("配捐比例请输入正整数！");
		            return;
		        }
		        dot = dividend.indexOf(".");
		        if(dot != -1){
		            alert("配捐比例请输入正整数！");
		            return;
		        }
		    }
			if(divisor =="0" || divisor==0 || dividend =="0" || dividend==0){
				alert("配捐比例不能输入0");
				return;
			}
			

			//如果minus被选中 , 减少金额不能大于已配捐金额
			if(mark == "" || mark == null){
				alert("备注说明不能为空！");
				return;
			}



			$.ajax({
				url:"/scfire/manage/saveBandFund",
				type:'POST',
				data:{
					  money : money , mark : mark , projectid : $("#projectid").val() , poolid : $("#types").val() , operate : $('input:radio[name="operate"]:checked').val() , regular : divisor+":"+dividend
					  },
				dataType:'json',
				async: false,
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				success:function(data){
					alert(data.info);
					location.reload();
				},
				error:function(data){
					alert("服务器内部错误！");
				}
			});

		}


	    
		$("#types").eq(0).change();
	</script>

</body>
</html>